﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TMS.ServiceProvider.CapitalTransportationSolutions.ShipRite
{
    public class RateProvider: ShipRiteBase
    {
        public static RatesList GetRates()
        {
            RatesList ratesList = null;

            XmlReaderSettings settings = new XmlReaderSettings() { IgnoreWhitespace = true };

            XmlReader xmlReader = FetchXmlReaderFromUrl(RatingRequestUrl, settings);

            if (xmlReader != null)
            {
                ratesList = Deserialize(xmlReader);
            }

            if (ratesList != null)
            {
                string strValue = ratesList.Error;
                if (!String.IsNullOrEmpty(strValue) && strValue != "0")
                {
                    throw new Exception(strValue);
                }
            }

            return ratesList;
        }

        public static RatesList Deserialize(XmlReader xmlReader)
        {
            RatesList ratesList = null;

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(RatesList));

            if (xmlSerializer.CanDeserialize(xmlReader))
            {
                ratesList = (RatesList)xmlSerializer.Deserialize(xmlReader);
            }

            return ratesList;
        }
    }
}
